package cm367;

import java.io.IOException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean
@SessionScoped
public class facilities {
	private int idFacilities = -1;
	private String FacilityCode = "";
	private String FacilityDesc = "";
	private String FacilityType = "Electronic Equipment";
	private String FacilityFormula = "";
	private double FacilityUnitPrice = 0.00;
	private String btnValue = "Insert";
	
	private boolean IsInsertRecord = false;
	private String ProcessType = "";
	private String oldFCode = "";
	private String oldFDesc = "";
	private String oldFType = "";
	private String oldFFormula = "";
	private double oldFUnitPrice = 0.00;
	

	public facilities() {
	}
	
	
	public int getIdFacilities() {
		return idFacilities;
	}
	public void setIdFacilities(int idFacilities) {
		this.idFacilities = idFacilities;
	}
	public String getFacilityCode() {
		return FacilityCode;
	}
	public void setFacilityCode(String facilityCode) {
		FacilityCode = facilityCode;
	}
	public String getFacilityDesc() {
		return FacilityDesc;
	}
	public void setFacilityDesc(String facilityDesc) {
		FacilityDesc = facilityDesc;
	}
	public String getFacilityType() {
		return FacilityType;
	}
	public void setFacilityType(String facilityType) {
		FacilityType = facilityType;
	}
	public String getFacilityFormula() {
		return FacilityFormula;
	}
	public void setFacilityFormula(String facilityFormula) {
		FacilityFormula = facilityFormula;
	}
	public double getFacilityUnitPrice() {
		return FacilityUnitPrice;
	}
	public void setFacilityUnitPrice(double facilityUnitPrice) {
		FacilityUnitPrice = facilityUnitPrice;
	}
	
	public String SaveFacility() throws IOException {
		
		FacilityList facilitylist = new FacilityList();
		List<Facility> RawData = facilitylist.getFacilitiesList();
		Facility fRec = new Facility();
		
	    if(idFacilities == -1) {
	    	facilitylist.setFacilitiesCount(facilitylist.getFacilitiesCount() + 1);
	    	fRec.setIdFacility(facilitylist.getFacilitiesCount());
	    	IsInsertRecord = true;
	    	ProcessType = "Inserted";	
	    } else {
	    	fRec.setIdFacility(idFacilities + 1);
			oldFCode = RawData.get(idFacilities).getFacilityCode();
			oldFDesc = RawData.get(idFacilities).getFacilityDesc();
			oldFType = RawData.get(idFacilities).getFacilityType();
			oldFFormula = RawData.get(idFacilities).getFacilityFormula();
			oldFUnitPrice = RawData.get(idFacilities).getFacilityUnitPrice();
	    	IsInsertRecord = false;
	    	ProcessType = "Modified";	
	    }
    	fRec.setFacilityCode(FacilityCode);
    	fRec.setFacilityDesc(FacilityDesc);
    	fRec.setFacilityFormula(FacilityFormula);
    	fRec.setFacilityType(FacilityType);
    	fRec.setFacilityUnitPrice(FacilityUnitPrice);
	    if(idFacilities == -1) {
	    	RawData.add(fRec);
	    } else {
	    	RawData.set(idFacilities, fRec);
	    }
		String[] rData =  new String[facilitylist.getFacilitiesCount()];
		for(int idx = 0; idx < facilitylist.getFacilitiesCount(); idx++) {
			rData[idx] = String.valueOf(RawData.get(idx).getIdFacility()).trim() + facilitylist.getDataSeparator().trim() +
							RawData.get(idx).getFacilityCode().trim()  + facilitylist.getDataSeparator().trim() +
							RawData.get(idx).getFacilityDesc().trim()  + facilitylist.getDataSeparator().trim() +
							RawData.get(idx).getFacilityType().trim()  + facilitylist.getDataSeparator().trim() +
							RawData.get(idx).getFacilityFormula().trim()  + facilitylist.getDataSeparator().trim() +
							String.valueOf(RawData.get(idx).getFacilityUnitPrice()).trim()  + facilitylist.getDataSeparator().trim();
		}
	    facilitylist.Write2File(rData);   	
	    
		btnValue = "Insert";
		return "fSave";
	}
	
	public String newFacility() {
		idFacilities = -1;
		FacilityCode = "";
		FacilityDesc = "";
		FacilityType = "Electronic Equipment";
		FacilityFormula = "";
		FacilityUnitPrice = 0.00;
		oldFCode = "";
		oldFDesc = "";
		oldFType = "";
		oldFFormula = "";
		oldFUnitPrice = 0.00;
		
		return "bFacility";
	}

	public boolean isIsInsertRecord() {
		return IsInsertRecord;
	}

	public void setIsInsertRecord(boolean isInsertRecord) {
		IsInsertRecord = isInsertRecord;
	}

	public String getProcessType() {
		return ProcessType;
	}

	public void setProcessType(String processType) {
		ProcessType = processType;
	}

	public String getOldFCode() {
		return oldFCode;
	}

	public void setOldFCode(String oldFCode) {
		this.oldFCode = oldFCode;
	}

	public String getOldFDesc() {
		return oldFDesc;
	}

	public void setOldFDesc(String oldFDesc) {
		this.oldFDesc = oldFDesc;
	}

	public String getOldFType() {
		return oldFType;
	}

	public void setOldFType(String oldFType) {
		this.oldFType = oldFType;
	}

	public String getOldFFormula() {
		return oldFFormula;
	}

	public void setOldFFormula(String oldFFormula) {
		this.oldFFormula = oldFFormula;
	}

	public double getOldFUnitPrice() {
		return oldFUnitPrice;
	}

	public void setOldFUnitPrice(double oldFUnitPrice) {
		this.oldFUnitPrice = oldFUnitPrice;
	}

	public boolean isValid()  {
		Double rst = -1.00;
		Facility fac = new Facility();
		fac.setFacilityFormula(FacilityFormula);
		fac.setFacilityUnitPrice(FacilityUnitPrice);
		try {
			rst = fac.Formula2Value(123);
		} catch (ArithmeticException ex) {
			rst = -999999.99;
		}
		btnValue = "Modify";
		return (rst > 0.00);
	}


	public String getBtnValue() {
		return btnValue;
	}


	public void setBtnValue(String btnValue) {
		this.btnValue = btnValue;
	}
}
